const baseDir = files.cwd()
let dir = "xiaola/"
if(baseDir.indexOf('xiaola') > 0) {
    dir = ""
}

const tools = require("./" + dir + "tools.js")
const api = require("./" + dir + "api.js")
const gps = require("./" + dir + "gps.js")
const orderManager = require("./" + dir + "orderManager.js")
const floatyInfo = require("./" + dir + "floatyInfo.js")
const floatyWin = require("./" + dir + "floatyWin.js")
const didi = require("./" + dir + "didi.js")

// 打开小拉APP
// launchApp("小拉出行司版");
console.clear()
console.log("==============开始执行============")
let appIsRun = false
// 设定截图开始坐标
const cutX = 50
const cutY = 250
const debug = 0
const scale = 0.5
let oldScreen = null

function init() {
    tools.makeDir("/sdcard/xiaola/")
    if(!debug)tools.requestScreenCapture() //请求获取截屏权限
    floatyWin.init((running) => {
        appIsRun = running
        running ? floatyInfo.show() : floatyInfo.hide()
    })
    // didi.init()
}


function getImg() {
    const screenImg = captureScreen()

    // 裁剪需要文字识别的区域
    const width = 1080 - cutX * 2
    const height = 2100 - cutY
    const topImg = images.clip(screenImg, cutX, cutY, width/4, 400)
    const topImgBase64 = tools.hash(images.toBase64(topImg))
    if(oldScreen == topImgBase64) {
        console.log("无更新")
        return false
    }
    oldScreen = topImgBase64

    const img = images.clip(screenImg, cutX, cutY, width, height)
    // images.save(img, "/sdcard/xiaola/" + (new  Date().getTime()) + ".png")
    // images.save(img, "/sdcard/xiaola/test.png")
    return img
}

// 识别图片文字
function getText(img) {    
    console.time('识别时间')
    let res = JSON.parse(JSON.stringify(paddle.ocr(img)))
    console.timeEnd('识别时间')
    return res
}


function run() {
    init()
    while(true) {
        if(!appIsRun) continue
        // let img = debug ? images.read("/sdcard/xiaola/new.png") : getImg()
        let img = debug ? images.read("/sdcard/DCIM/xl_1762091850529.jpg") : getImg()
        if(!img) continue // 屏幕没有发生变化
        img = images.scale(img, scale, scale)
        let text = getText(img)
        let orders = orderManager.parse(text, cutX, cutY, scale)
        // if(debug) exit()
        // console.log(orders)
        for (let order of orders) {
            
            // 如果终点地址少于3个字，那么是解析出错了，截图保留
            if (order.addressEnd.length < 3) {
                images.save(img, "/sdcard/xiaola/new.png")
                continue
            }


            // 起点距离大于3公里、行程距离小于4公里、场站 忽略订单
            if(order.distanceBegin > 2 || order.distanceEnd < 4 || order.orderType.indexOf('场站') > 0 || order.addressEnd.length > 30)  
                continue 
            
            let oldOrder = orderManager.find(order)
            if(oldOrder) {
                log("订单已存在")
                floatyInfo.showInfo(oldOrder)
                continue
            }

            // currentGPS = gps.getCurrentGPS()
            if(!order.addressBegin || !order.addressEnd || !order.addressEndRegion) {
                console.error('地址信息不完整')
                console.error(order)
                continue
            }

            let pathInfo = api.getPathInfo(order.addressBegin, order.addressEndRegion + order.addressEnd)
            if(typeof pathInfo == "string") {
                Object.assign(order, { distance: 0, time: 0 })
            } else {
                Object.assign(order, pathInfo)
             }
            
            orderManager.add(order)
            floatyInfo.showInfo(order)
            // break //只处理一个订单
        }
    }    
}

run()
// draw(500, 500, "hello")
// api.addressToGPS('未来方舟分校东北侧')
// let currentGPS = gps.getCurrentGPS()
// let address = api.GPSToAddress(currentGPS.longitude + "," + currentGPS.latitude)